<?php
session_start();
?>
<script src="../scriptaculous/lib/prototype.js" type="text/javascript"></script>
<script type="text/javascript" src="../push.js" language="javascript"></script>
<script type="text/javascript" src="../scripts.js" language="javascript"></script>
<?php 

require_once('tOAuth/tOAuth.class.php');
require_once('TwitterSearch.class.php');
require('config.php');
include('../Connections/conn.php');
$consulta="select * from twitter";
$res=mysql_query($consulta,$conexion);
$r=mysql_fetch_array($res);
$_SESSION['toauth_at']=$r['toauth_at'];
$_SESSION['toauth_ats']=$r['toauth_ats'];


if(empty($_SESSION['toauth_at']) || empty($_SESSION['toauth_ats'])) 
{
	if(!empty($_GET['oauth_token']) && $_SESSION['toauth_state'] == 'start') 
	{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_rt'], $_SESSION['toauth_rts']);
		$a = $connection->authenticate(false);
		$_SESSION['toauth_at'] = $a['oauth_token'];
		$_SESSION['toauth_ats'] = $a['oauth_token_secret'];
		$_SESSION['toauth_state'] = 'returned';
	} 
	else 
	{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET);
		$a = $connection->authenticate(true);
		$_SESSION['toauth_rt'] = $a['oauth_token'];
		$_SESSION['toauth_rts'] = $a['oauth_token_secret'];
		$_SESSION['toauth_state'] = 'start';
	}
}


function redirigir()
{
if(!empty($_SESSION['toauth_at']) && !empty($_SESSION['toauth_ats'])) 
{
	?>
	<script type="text/javascript">redir('get.php','tw',50);</script><br />
    <?php
}
}
/*if(empty($_SESSION['toauth_at']) || empty($_SESSION['toauth_ats'])) {
	header('Location: signout.php');
	die();
}*/


function enviar_twitter($msj)
{
	$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r2 = $connection->post('statuses/update', array('status'=>$msj));
}

function mostrar_muro()
{
	$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	//$r1 = $connection->get('statuses/mentions');
	$r1 = $connection->get('statuses/home_timeline');
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;
}

function mostrar_menciones()
{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	$r1 = $connection->get('statuses/mentions');
//	$r1 = $connection->get('statuses/home_timeline');
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;	

}

function mostrar_privados_recibidos()
{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	$r1 = $connection->get('direct_messages');
//	$r1 = $connection->get('statuses/home_timeline');
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;	

}
function mostrar_privados_enviados()
{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	$r1 = $connection->get('direct_messages/sent');
//	$r1 = $connection->get('statuses/home_timeline');
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;	

}

function seguidores()
{
	$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	$r1 = $connection->get('statuses/followers');
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;	
}

function amigos()
{
	$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	$r1 = $connection->get('statuses/friends');
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;	
}


function buscar_menciones($texto)
{
	$search = new TwitterSearch();
	$search->contains($texto);
	$results = $search->rpp(100)->results();
	return $results;
//	$results = $search->from('ryanfaerman')->with('flsccc')->results();
}

function buscar_hash($texto)
{
	$search = new TwitterSearch();
	$search->with($texto);
	$results = $search->rpp(100)->results();
		return $results;
//	$results = $search->from('ryanfaerman')->with('flsccc')->results();
}

function buscar_palabra($texto)
{
	$search = new TwitterSearch($texto);
	$results = $search->rpp(100)->results();
		return $results;
//	$results = $search->from('ryanfaerman')->with('flsccc')->results();
}

function perfil($id)
{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	$r1 = $connection->get('users/show',array("user_id"=>$id));
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r1;	
}

function miperfil()
{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r = $connection->get('account/verify_credentials');
	//$r1 = $connection->get('users/show',array("user_id"=>$id));
	if(isset($r['error'])) 
		die('<b>Error:</b> '.$r['error']);
	return $r;	
}

function enviar_privado($id,$texto)
{
		$connection = new tOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['toauth_at'], $_SESSION['toauth_ats']);
	$r1 = $connection->post('direct_messages/new', array('screen_name'=>$id,'text'=>$texto));
	return $r1;	
}
